<?php

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller as BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

// 地址的查询
class AddressController extends BaseController
{
    public function __construct()
    {
    }

    // 查询省市区
    public function index()
    {
        // 查询的所有的
        $prvince = DB::table('address_provinces')->get();
        $city = DB::table('address_cities')->get();
        $area = DB::table('address_areas')->get();
        $data = [
            'prvince' => $prvince,
            'city' => $city,
            'area' => $area
        ];
        return $this->returnMsg(200, '查询成功', $data);
    }

    public function provinceAndCity(Request $request)
    {
        // 查询的所有的
        $prvince = DB::table('address_provinces')->get();
        $city = DB::table('address_cities')->get();
        // $area = DB::table('address_areas')->get();
        $prvince = json_decode(json_encode($prvince), true);
        $city = json_decode(json_encode($city), true);
        // $area = json_decode(json_encode($area), true);
        $array = [];
        $arr = [];
        $defaultProvince = $request->get('province');
        $defaultCity = $request->get('city');
        foreach ($prvince as $key => $v) {
            $array[$key] = $v;
            $name = str_replace(['省', '市', '自治区', '壮族'], '', $v['name']);
            if (!empty($defaultProvince) && strpos($defaultProvince, $name) !== false) {
                $defaultProvince = $v['name'];
            }
            // $arr[$key] = $v;
            foreach ($city as $ks => $vs) {
                $name = str_replace(['省', '市', '壮族'], '', $vs['name']);
                if (!empty($defaultCity) && strpos($defaultCity, $name) !== false) {
                    $defaultCity = $vs['name'];
                }
                if ($vs['pid'] == $v['id']) {
                    $array[$key]['children'][] = $vs;
                    // $arr[$key]['children'][]  = $vs;
                    // foreach ($area as $kone => $vone) {
                    //   // $arr[$key]['children'][$ks]['children'][] = $vone;
                    // }
                }
            }
        }
        return $this->returnMsg(200, $defaultProvince . ',' . $defaultCity, $array);
    }

    public function provinceAndCityAndArea()
    {
        // 查询的所有的
        $prvince = DB::table('address_provinces')->get();
        $city = DB::table('address_cities')->get();
        $area = DB::table('address_areas')->get();
        $prvince = json_decode(json_encode($prvince), true);
        $city = json_decode(json_encode($city), true);
        $area = json_decode(json_encode($area), true);
        $array = [];
        foreach ($prvince as $key => $v) {
            $array[$key] = $v;
            foreach ($city as $ks => $vs) {
                if ($vs['pid'] == $v['id']) {
                    $array[$key]['children'][$ks] = $vs;
                    foreach ($area as $kone => $vone) {
                        if ($vone['pid'] == $vs['id']) {
                            $array[$key]['children'][$ks]['children'][] = $vone;
                        }
                    }
                }
            }
        }
        return $this->returnMsg(200, '查询成功', $array);
    }

    // 获取省
    public function getProvinces()
    {
        $prvince = DB::table('address_provinces')->get();
        return $this->returnMsg(200, '查询成功', $prvince);
    }

    // 获取市
    public function getCitys()
    {
        $prvince = DB::table('address_cities')->get();
        return $this->returnMsg(200, '查询成功', $prvince);
    }

    // 获取区
    public function getAreas()
    {
        $prvince = DB::table('address_areas')->get();
        return $this->returnMsg(200, '查询成功', $prvince);
    }
}
